import hawk from "./browser.js"

function wx_request(url, data = {},method='get',header = {},returnBlock) {


  let id = "nVr6KZ0b"
  let token = "GmYm1v8xsktCDzAcouJnkVK4PwNJDuh2"

  console.log(url)

  header["Authorization"] = addToken(id,token,url,method)
  header["api-version"] = "v3.5.0"

  console.log(header)

  wx.request({
    url: url,
    data: data,
    header: header,
    method: method,
    success(res) {
      console.log(res.data)
      return returnBlock(res.data)
    }
  })
}

function addToken(id,token,url,method) {
  let credentials = {
    id: id,
    key: token,
    algorithm: 'sha256'
  }

  let hawkHeader = hawk.client.header(url,method,{
    credentials: credentials
  })

  console.log(hawkHeader.header)

  return hawkHeader.header
}

module.exports = {
  wx_request: wx_request
}